home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / magazi~1 / 334 / box.c next >
Encoding:
C/C++ Source or Header  |  1988-11-04  |  9.1 KB  |  327 lines

  1. /********************************************************/
  2. /*                C-manship, Listing 1                  */
  3. /*                     ST-Log #25                       */
  4. /*              Developed with Megamax C                */
  5. /********************************************************/
  6. #include <obdefs.h>
  7. #include <gemdefs.h>
  8.  
  9. #define TRUE    1
  10. #define FALSE   0
  11. #define SOLID   1
  12. #define PATTERN 2
  13. #define LEFT    1
  14. #define RIGHT   2
  15.  
  16. /* GEM arrays */
  17. int work_in[11], work_out[57], contrl[12], intin[128], 
  18.     ptsin[128], intout[128], ptsout[128];
  19.  
  20. int handle,    /* Application handle. */
  21.     dum;       /* Dummy storage.      */
  22.  
  23.  
  24. /******************************************************
  25. * Main program.
  26. ******************************************************/
  27. main ()
  28. {
  29.    appl_init ();           /* Init application.         */
  30.    open_vwork ();          /* Open virtual workstation. */
  31.    do_box ();              /* Go do our thing.          */
  32.    v_clsvwk ( handle );    /* Close virtual workstation.*/
  33.    appl_exit ();           /* Back to the desktop.      */
  34. }
  35.  
  36.  
  37. /******************************************************
  38. * do_box ()
  39. * Calls screen setup function and handles the mouse,
  40. * calling the appropriate box functions based on the
  41. * mouse's coordinates and button state.
  42. ******************************************************/
  43. do_box ()
  44. {
  45.    int mouse_x,    /* Mouse X coordinate.     */
  46.        mouse_y,    /* Mouse Y coordinate.     */
  47.        mouse_but,  /* Mouse button state.     */
  48.        box_x,      /* Selected box X coord.   */
  49.        box_y,      /* Selected box Y coord.   */
  50.        box_width,  /* Selected width of box.  */
  51.        box_height, /* Selected height of box. */ 
  52.        exit,       /* Program exit flag.      */
  53.        coords_ok;  /* Proper coordinates flag.*/
  54.  
  55.    setup_scrn ();
  56.    coords_ok = FALSE;
  57.    graf_mouse ( ARROW, 0L );
  58.  
  59.    /* Wait for box coordinates within the boundary. */
  60.    while ( !coords_ok ) {
  61.  
  62.       /* Poll for left button press. */
  63.       mouse_but = 0;
  64.       while ( mouse_but != LEFT )
  65.          graf_mkstate ( &mouse_x, &mouse_y, &mouse_but, &dum );
  66.  
  67.       /* Get coordinates for the box. */
  68.       box_x = mouse_x;
  69.       box_y = mouse_y;
  70.       graf_rubberbox ( box_x, box_y, 20, 20, &box_width, &box_height );
  71.  
  72.       /* Allow only a box whose size fits within the */
  73.       /* boundary to be drawn.                       */
  74.       if ( box_x > 20 && box_x + box_width < work_out[0]-21 &&
  75.            box_y > 20 && box_y + box_height < work_out[1]-21 ) {
  76.          draw_box ( box_x, box_y, box_width, box_height );
  77.          coords_ok = TRUE;
  78.       }
  79.    }
  80.    exit = FALSE;
  81.    while ( !exit ) {
  82.       mouse_but = 0;
  83.  
  84.       /* Wait for press of left or right mouse button. */
  85.       while ( mouse_but != LEFT && mouse_but != RIGHT )
  86.          graf_mkstate ( &mouse_x, &mouse_y, &mouse_but, &dum );
  87.  
  88.       if ( mouse_but == LEFT )
  89.  
  90.          /* If the mouse was on the sizing button, */
  91.          /* allow the user to resize the box.      */
  92.          if ( chose_size ( mouse_x, mouse_y, box_x, box_y, 
  93.                            box_width, box_height ) )
  94.             size_box ( box_x, box_y, &box_width, &box_height );
  95.  
  96.          /* If the mouse was anywhere else in the */
  97.          /* box, allow the user to move the box.  */
  98.          else if ( chose_move ( mouse_x, mouse_y, box_x, box_y, 
  99.                                   box_width, box_height ) )
  100.             move_box ( &box_x, &box_y, box_width, box_height );
  101.  
  102.       if ( mouse_but == RIGHT )
  103.          exit = TRUE;
  104.    }
  105. }
  106.  
  107.  
  108. /******************************************************
  109. * draw_box ()
  110. * Draws a shaded box with a button in the lower right
  111. * corner.  The input is the X and Y coordinates of
  112. * the box's upper left corner and its width and height.
  113. ******************************************************/
  114. draw_box ( x, y, w, h )
  115. int x, y, w, h;
  116. {
  117.    int pxy[4];
  118.  
  119.    graf_mouse ( M_OFF, 0L );
  120.  
  121.    /* Draw the main body of the box. */
  122.    vsf_interior ( handle, PATTERN );
  123.    vsf_style ( handle, 5 );
  124.    vsf_color ( handle, BLACK );
  125.    pxy[0] = x;
  126.    pxy[1] = y;
  127.    pxy[2] = x + w - 1;
  128.    pxy[3] = y + h - 1;
  129.    v_bar ( handle, pxy );
  130.  
  131.    /* Draw the box's sizing button. */
  132.    vsf_interior ( handle, SOLID );
  133.    pxy[0] = x + w - 10;
  134.    pxy[1] = y + h - 10;
  135.    v_bar ( handle, pxy );
  136.  
  137.    graf_mouse ( M_ON, 0L ); 
  138. }
  139.  
  140.  
  141. /******************************************************
  142. * size_box ()
  143. * Resizes a box.  The input is the X and Y coordinates
  144. * of the box and pointers to its width and height.  The
  145. * function returns the new width and height by way
  146. * of the pointers, thus replacing the old values of 
  147. * the width and height.
  148. ******************************************************/
  149. size_box ( x, y, w, h )
  150. int x, y, *w, *h;
  151. {
  152.    int old_w, old_h;
  153.    int pxy[4];
  154.  
  155.    old_w = *w;
  156.    old_h = *h;
  157.  
  158.    /* Get the new box size. */
  159.    graf_rubberbox ( x, y, 20, 20, w, h );
  160.  
  161.    /* Don't allow the new box to exceed the boundary. */
  162.    if ( x + *w > work_out[0]-20 | y + *h > work_out[1]-20 ) {
  163.       *w = old_w;
  164.       *h = old_h;
  165.    }
  166.  
  167.    /* If the size is okay, draw the box. */
  168.    else {
  169.       draw_box ( x, y, *w, *h );
  170.  
  171.       /* Erase the leftover portions (if */
  172.       /* any) of the old box.            */
  173.       graf_mouse ( M_OFF, 0L );
  174.       vsf_interior ( handle, SOLID );
  175.       vsf_color ( handle, WHITE );
  176.       if ( *w < old_w ) {
  177.          pxy[0] = x + *w;
  178.          pxy[1] = y;
  179.          pxy[2] = x + old_w - 1;
  180.          pxy[3] = y + old_h - 1;
  181.          v_bar ( handle, pxy );
  182.       }
  183.       if ( *h < old_h ) {
  184.          pxy[0] = x;
  185.          pxy[1] = y + *h;
  186.          pxy[2] = x + old_w - 1;
  187.          pxy[3] = y + old_h - 1;
  188.          v_bar ( handle, pxy );
  189.       }
  190.       graf_mouse ( M_ON, 0L );
  191.    }
  192. }
  193.  
  194.  
  195. /******************************************************
  196. * move_box ()
  197. * Repositions a box.  The input is a pointer to the
  198. * box's X coord., a pointer to the box's Y coord., and 
  199. * the box's width and height.  The new X and Y 
  200. * coordinates are returned from the function by way of 
  201. * the pointers, thus replacing the old X and Y values.
  202. ******************************************************/
  203. move_box ( x, y, w, h )
  204. int *x, *y, w, h;
  205. {
  206.    int old_x, old_y;
  207.    int pxy[4];
  208.  
  209.    old_x = *x;
  210.    old_y = *y;
  211.    graf_mouse ( FLAT_HAND, 0L );
  212.  
  213.    /* Get new location for the box. */
  214.    graf_dragbox ( w, h, *x, *y, 21, 21, 
  215.                   work_out[0]-41, work_out[1]-41, x, y );
  216.  
  217.    /* Erase the old box. */
  218.    graf_mouse ( M_OFF, 0L );
  219.    vsf_color ( handle, WHITE );
  220.    vsf_interior ( handle, SOLID );
  221.    pxy[0] = old_x;
  222.    pxy[1] = old_y;
  223.    pxy[2] = old_x + w - 1;
  224.    pxy[3] = old_y + h - 1;
  225.    v_bar ( handle, pxy );
  226.  
  227.    /* Draw the new box. */
  228.    draw_box ( *x, *y, w, h );
  229.  
  230.    graf_mouse ( M_ON, 0L );
  231.    graf_mouse ( ARROW, 0L );
  232. }
  233.  
  234.  
  235. /******************************************************
  236. * chose_size ()
  237. * Returns a boolean value based on whether the mouse
  238. * button was pressed while over the box's sizing
  239. * button.  The input is the X and Y coordinates of
  240. * the mouse, the X and Y coordinates of the box, and
  241. * the width and height of the box.
  242. ******************************************************/
  243. chose_size ( mx, my, bx, by, bw, bh )
  244. int mx, my, bx, by, bw, bh;
  245. {
  246.    if ( mx>bx+bw-10 && mx<bx+bw && my>by+bh-10 && my<by+bh )
  247.       return ( TRUE );
  248.    else
  249.       return ( FALSE );
  250. }
  251.  
  252.  
  253. /******************************************************
  254. * chose_move ()
  255. * Returns a boolean value based on whether the mouse
  256. * button was pressed while over an area of the box
  257. * other than the sizing button.  The input is the X 
  258. * and Y coordinates of the mouse, the X and Y 
  259. * coordinates of the box, and the width and height of 
  260. * the box.
  261. ******************************************************/
  262. chose_move ( mx, my, bx, by, bw, bh )
  263. int mx, my, bx, by, bw, bh;
  264. {
  265.    if ( mx>bx && mx<bx+bw && my>by && my<by+bh )
  266.       return ( TRUE );
  267.    else
  268.       return ( FALSE );
  269. }
  270.  
  271.  
  272. /******************************************************
  273. * setup_scrn ()
  274. * Prepares the screen by clearing the workstation and
  275. * drawing a border.
  276. ******************************************************/
  277. setup_scrn ()
  278. {
  279.    int pxy[10];
  280.  
  281.    graf_mouse ( M_OFF, 0L );
  282.  
  283.    /* Erase the screen. */
  284.    v_clrwk ( handle );
  285.  
  286.    /* Draw the border. */
  287.    pxy[0] = 20;
  288.    pxy[1] = 20;
  289.    pxy[2] = work_out[0] - 20;
  290.    pxy[3] = 20;
  291.    pxy[4] = work_out[0] - 20;
  292.    pxy[5] = work_out[1] - 20;
  293.    pxy[6] = 20;
  294.    pxy[7] = work_out[1] - 20;
  295.    pxy[8] = 20;
  296.    pxy[9] = 20;
  297.    v_pline ( handle, 5, pxy );
  298.  
  299.    graf_mouse ( M_ON, 0L );
  300. }
  301.  
  302.  
  303. /*****************************************************
  304. * open_vwork ()
  305. * Opens a virtual workstation.
  306. *****************************************************/
  307. open_vwork ()
  308. {
  309.    int i;
  310.  
  311.    /* Get graphics handle, initialize the GEM arrays and open  */
  312.    /* a virtual workstation.                                   */
  313.  
  314.    handle = graf_handle ( &dum, &dum, &dum, &dum);
  315.    for ( i=0; i<10; work_in[i++] = 1 );
  316.    work_in[10] = 2;
  317.    v_opnvwk ( work_in, &handle, work_out );
  318. }
  319.  
  320.